/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2013 All Rights Reserved.
 */
package com.pingan.mobilegw.core.service.invoke.proxy;

import com.pingan.SpringContextUtil;
import com.pingan.mobilegw.adapterservice.*;
import com.pingan.mobilegw.core.model.ServiceConfig;
import com.pingan.mobilegw.core.model.comm.TargetRequestObject;
import com.pingan.mobilegw.core.model.exception.MgwException;
import com.pingan.mobilegw.core.service.util.DynamicTimeOutUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.lang.reflect.UndeclaredThrowableException;
import java.util.List;

/**
 * Mofa的Spi调用代理
 * 
 * @author jiadao
 * @version $Id: MobileAdapterServiceInvokeProxyImpl.java, v 0.1 2013-4-8 下午9:22:58 jiadao Exp $
 */
@Component
public class MobileAdapterServiceInvokeProxyImpl implements MofaInvokeProxy {

    /** logger */
    private static final Log logger = LogFactory
                                           .getLog(MobileAdapterServiceInvokeProxyImpl.class);

    private MobileAdapterService mobileAdapterService;

    public TargetResponseObject invoke(TargetRequestObject targetObject) {
        ServiceConfig serviceConfig = targetObject.getServiceConfig();


        //trSyncServiceFactory
           // .getService(DynamicTimeOutUtil.getServiceKey(serviceConfig.getSysName(),
             //   DynamicTimeOutUtil.getTimeOut(serviceConfig)));

        try {
            // 远程调用
            mobileAdapterService= (MobileAdapterService) SpringContextUtil.getBean("mobileAdapterService");
            return mobileAdapterService.mobileService(targetObject.getUniqueId(), targetObject
                .getMobileContext(), targetObject.getRequestData());

        } catch (UndeclaredThrowableException e) {
           //TODO LoggerUtil.error(logger, e, "远程调用服务异常，operationType=",
             //   serviceConfig.getOperationType(), ",SysName=", serviceConfig.getSysName(),
               // ",InterfaceName=", serviceConfig.getInterfaceType(), ",MethodName=", serviceConfig
                 //   .getMethodName());

            // 调用超时
       //TODO     if (e.getUndeclaredThrowable() instanceof HSFTimeOutException) {
             //   throw new MgwException(ResultEnum.RequestTimeOut, e);
           // }
            // 其他调用异常
            throw new MgwException(ResultEnum.RemoteAccessException, e);
        }

    }


}
